package me.jiatao.springmvc.B_urlmapper;

import me.jiatao.springmvc.pojo.FormBean;
import me.jiatao.springmvc.pojo.User;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping(value = "mapper")
public class UrlMapper {

    /**
     * 对应的请求映射应该是mapper/show1.do
     */
    @RequestMapping(value = "/show1")
    // 利用requestMapping配置对应的请求路径
    public ModelAndView test1() {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("hello");
        mv.addObject("msg", "这是第一个注解程序");
        return mv;
    }

    /**
     * * 匹配0或者任意数量的字符
     *
     * @return
     */
    @RequestMapping(value = "/test/*/show2.do")
    public ModelAndView test2() {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("msg", "这是*方式的映射");
        return mv;
    }

    /**
     * ? 匹配任何单字符,不能为空
     *
     * @return
     */
    @RequestMapping(value = "/test/abc?/show3.do")
    public ModelAndView test3() {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("msg", "这是?方式的映射");
        return mv;
    }

    /**
     * ** 匹配0或者更多的目录
     *
     * @return
     */
    @RequestMapping(value = "/test/**/show4.do")
    public ModelAndView test4() {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("msg", "这是**方式的映射");
        return mv;
    }


    /**
     * 占位符方法
     *
     * @param abc
     * @return
     */
    @RequestMapping(value = "/test/{abc}/show5")
    public ModelAndView test5(@PathVariable("abc") String abc) {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("msg", "这是占位符方式的映射:" + abc);
        return mv;
    }

    /**
     * 占位符
     *
     * @param itemId
     * @param itemprice
     * @return
     */
    @RequestMapping(value = "/item/{itemId}/{itemprice}")
    public ModelAndView test6(@PathVariable("itemId") String itemId,
                              @PathVariable("itemprice") Double itemprice) {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("msg", "这是占位符方式的映射:" + itemId + ",price:" + itemprice);
        return mv;
    }

    /**
     * 限定请求方式
     *
     * @return
     */
    @RequestMapping(value = "/show7", method = {RequestMethod.GET, RequestMethod.POST})
    public ModelAndView test7() {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("msg", "限定只能是Get和Post请求");
        return mv;
    }


    /**
     * 限定参数必须包含某个值
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "show8", params = "userId")
    public ModelAndView test8(@RequestParam("userId") String userId) {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("msg", "限定请求参数：userId:" + userId);
        return mv;
    }

    /**
     * 限定必须包括某个值，且不能为特定值
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "show9", params = "userId!=1")
    public ModelAndView test9(@RequestParam("userId") String userId) {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("msg", "限定请求参数：userId:" + userId);
        return mv;
    }


    /**
     * 接受request，response，session
     *
     * @return
     */
    @RequestMapping(value = "show10")
    public ModelAndView test10(HttpServletRequest request, HttpServletResponse response, HttpSession session) {
        ModelAndView mv = new ModelAndView("hello");
        StringBuffer sb = new StringBuffer();
        sb.append("request:" + request + "<br/>");
        sb.append("response:" + response + "<br/>");
        sb.append("session:" + session + "<br/>");
        mv.addObject("msg", sb.toString());
        return mv;
    }


    /**
     * 获取占位符的数据
     * PathVariable里面的值不能省略
     *
     * @return
     */
    @RequestMapping(value = "show11/{userId}")
    public ModelAndView test11(@PathVariable("userId") Integer userId) {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("msg", "userId: " + userId);
        return mv;
    }

    /**
     * required标识一个参数是否必须存在，默认是true，标识必须要有 defaultValue :
     * 如果没有传递该参数，那么默认的值就是defaultValue对应的值 如果设置了 defaultValue required 失效
     *
     * @param userId
     * @return
     */
    @RequestMapping(value = "show12")
    public ModelAndView test12(
            @RequestParam(value = "userId", required = true, defaultValue = "1") Integer userId) {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("msg", "userId: " + userId);
        return mv;
    }

    /**
     * 获取cookie的值
     *
     * @param jsessionId
     * @return
     */
    @RequestMapping(value = "show13")
    public ModelAndView test13(@CookieValue(value = "JSESSIONID", required = false) String jsessionId) {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("msg", "JSESSIONID: " + jsessionId);
        return mv;
    }

    /**
     * 使用pojo直接接受请求过来的参数
     * 只能通过URL传值
     *
     * @return
     */
    @RequestMapping(value = "show14")
    public ModelAndView test14(User user) {
        ModelAndView mv = new ModelAndView("hello");
        mv.addObject("msg", "user:" + user);
        return mv;
    }

    /**
     * 接受form表单参数
     *
     * @param name
     * @param age
     * @param income
     * @param isMarried
     * @param interests
     * @return
     */
    @RequestMapping(value = "show15")
    public ModelAndView test15(@RequestParam("name") String name,
                               @RequestParam("age") Integer age,
                               @RequestParam("income") Double income,
                               @RequestParam("isMarried") Boolean isMarried,
                               @RequestParam("interests") String[] interests) {
        ModelAndView mv = new ModelAndView("hello");

        StringBuffer sb = new StringBuffer();
        sb.append("name:" + name + "<br/>");
        sb.append("age:" + age + "<br/>");
        sb.append("income:" + income + "<br/>");
        sb.append("isMarried:" + isMarried + "<br/>");
        for (String interest : interests) {
            sb.append("interests:" + interest + "<br/>");
        }
        mv.addObject("msg", sb.toString());
        return mv;
    }

    /**
     * List集合映射
     *
     * @param bean
     * @return
     */
    @RequestMapping(value = "show16")
    public ModelAndView test16(FormBean bean) {
        ModelAndView mv = new ModelAndView("hello");

        for (User user : bean.getUsers()) {
            System.out.println(user);
        }

        mv.addObject("users", bean.getUsers());
        return mv;
    }

    /**
     * 使用jstl
     *
     * @return
     */
    @RequestMapping(value = "show17")
    public ModelAndView test17() {
        ModelAndView mv = new ModelAndView("hello");

        // 定义多个User对象，传递到jsp中，在jsp中进行显示
        List<User> users = new ArrayList<>();

        User user1 = new User();
        user1.setId(1l);
        user1.setAge(19);
        user1.setName("张三");

        users.add(user1);

        User user2 = new User();
        user2.setId(2l);
        user2.setAge(19);
        user2.setName("奉先");

        users.add(user2);

        User user3 = new User();
        user3.setId(1l);
        user3.setAge(19);
        user3.setName("貂蝉");

        users.add(user3);

        mv.addObject("users", users);

        return mv;
    }


    // {username:,age:..}

    /**
     * 输出json
     * <p>
     * 只需要在方法的返回值定义要返回的json数据的原始内容
     * 需要在方法的商品@responsebody
     * <p>
     * 需要添加jackson的依赖
     *
     * @return
     */
    @RequestMapping(value = "show18")
    @ResponseBody
    public User test18() {
        User user1 = new User();
        user1.setId(1l);
        user1.setAge(19);
        user1.setName("张三");
        return user1;
    }

    /**
     * 输出json集合
     *
     * @return
     */
    @RequestMapping(value = "show19")
    @ResponseBody
    public List<User> test19() {
        List<User> users = new ArrayList<>();

        User user1 = new User();
        user1.setId(1l);
        user1.setAge(19);
        user1.setName("张三");

        users.add(user1);

        User user2 = new User();
        user2.setId(2l);
        user2.setAge(19);
        user2.setName("奉先");

        users.add(user2);

        User user3 = new User();
        user3.setId(1l);
        user3.setAge(19);
        user3.setName("貂蝉");

        users.add(user3);
        return users;
    }


    /**
     * 在参数之前加上@RequestBody ,就可以接受json的数据
     * @param users
     * @return
     */
    @RequestMapping(value="show20")
    public ModelAndView test20(@RequestBody List<User> users){
        ModelAndView mv = new ModelAndView("hello");
        System.err.println(users);
        mv.addObject("msg", users);
        return mv;
    }
    /**
     *  接受一个请求，但是该请求没有任何的响应内容，
     */

    @RequestMapping(value="show21")
    @ResponseStatus(value= HttpStatus.NOT_FOUND)
    public void test21(@RequestBody List<User> users){
        for(User user : users){
            System.err.println(user);
        }
    }

    @RequestMapping("/upload")
    public String upload(@RequestParam("file") MultipartFile multipartFile)
            throws Exception {
        if (multipartFile != null) {
            // multipartFile.getOriginalFilename() 获取文件的原始名称
            multipartFile.transferTo(new File("D:\\JavaWeb\\@upload\\" + multipartFile.getOriginalFilename()));
        }
        return "redirect:/success.html";
    }

    @RequestMapping("/upload2")
    public String upload2(@RequestParam("files") MultipartFile[] multipartFiles)
            throws Exception {
        if(multipartFiles != null){
            for(MultipartFile multipartFile : multipartFiles){
                multipartFile.transferTo(new File("D:\\JavaWeb\\@upload\\" + multipartFile.getOriginalFilename()));
            }
        }
        return "redirect:/success.html";
    }
}
